import FYResMgr from "./FYResMgr";
import FYTips from "./FYTips";
import FYGlobalVarMgr from "./FYGlobalVarMgr";

/** 提示管理器 */
export default class FYTipsMgr {
    public static readonly Instance: FYTipsMgr = new FYTipsMgr();
    private constructor() { }

    /** 容器 */
    private _nodeContainer: cc.Node = null;
    /** 对象池 */
    private _pool: cc.NodePool = new cc.NodePool();

    public init(nodeContainer: cc.Node) {
        this._nodeContainer = nodeContainer;
    }

    private async getTips(): Promise<cc.Node> {
        let nodeTips: cc.Node = null;
        if (this._pool.size() > 0) {
            nodeTips = this._pool.get();
        } else {
            nodeTips = cc.instantiate(await FYResMgr.Instance.loadOtherSync("FYTips"));
        }
        return nodeTips;
    }

    private putTips(nodeTips: cc.Node) {
        if (!nodeTips) {
            cc.error("nodeTips is null");
            return;
        }
        this._pool.put(nodeTips);
    }

    /**
     * 显示分数动画
     * @param tips 提示内容
     */
    public async showTips(tips: string) {
        let fyTips = await this.addTips();
        if (fyTips) {
            fyTips.setTips(tips);
        } else {
            console.log("---- Tips 创建加载失败 ----")
        }
    }

    public async addTips(parent: cc.Node = this._nodeContainer || FYGlobalVarMgr.Instance.NodeTop): Promise<FYTips> {
        console.log("---- Tips 添加addTips ----")
        let nodeTips: cc.Node = await this.getTips();
        if (nodeTips) {
            nodeTips.parent = parent;
            nodeTips.scale = 1;
            nodeTips.opacity = 255;
            let tips = nodeTips.getComponent(FYTips);
            return tips;
        } else {
            return null;
        }
    }

    public removeTips(scoreTween: FYTips) {
        this.putTips(scoreTween.node);
    }
}
